package com.rf.richfitwheel.admin.utils;

import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.common.contants.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class UserUtil {

    //操作人
    public static String getUserName() {
        Session session = SecurityUtils.getSubject().getSession();
        UserVO userVO = (UserVO) session.getAttribute(Constant.USERSESSION);
        String username = userVO.getUsername();
        return username;
    }

    //当前登录人名称
    public static String getStaffName() {
        Session session = SecurityUtils.getSubject().getSession();
        UserVO userVO = (UserVO) session.getAttribute(Constant.USERSESSION);
        String staffName = userVO.getStaffName();
        return staffName;
    }

    //当前登录人部门编号
    public static String getOrgId() {
        Session session = SecurityUtils.getSubject().getSession();
        UserVO userVO = (UserVO) session.getAttribute(Constant.USERSESSION);
        String orgId = userVO.getOrgId();
        return orgId;
    }

    // 获取当前的登陆用户对象
    public static UserVO getUser() {
        Session session = SecurityUtils.getSubject().getSession();
        UserVO userVO = (UserVO) session.getAttribute(Constant.USERSESSION);
        if (null != userVO) {
            userVO.setSessionId("shiro:session:" + session.getId().toString());
        }
        return userVO;
    }
    public static UserVO getUser(String sessionId) {
//        JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "192.168.137.202", 6380, 3000000, "1qaz2wsx", 0);
        JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), RedisUtil.getHost(), Integer.parseInt(RedisUtil.getPort()), 3000000, RedisUtil.getPassword(), Integer.parseInt(RedisUtil.getDatabase()));
        //获取连接资源
        Jedis resource = jedisPool.getResource();
        byte[] hello = resource.get(sessionId.getBytes());//.get("shiro:session:6385811a-7498-45cf-abb9-cda2f9339864");
        Session session = byteToSession(hello);
        UserVO userVO = (UserVO) session.getAttribute(Constant.USERSESSION);
        userVO.setSessionId(session.getId().toString());
        return userVO;
    }

    public static void setUser(UserVO userVO) {
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(Constant.USERSESSION, userVO);
    }

    public static void main(String[] args) {
        //创建连接池对象  host:连接redis主机IP ；port:redis的默认端口
        JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "192.168.137.202", 6380, 3000000, "1qaz2wsx", 0);
        //获取连接资源
        Jedis resource = jedisPool.getResource();
        byte[] hello = resource.get("shiro:session:765de358-d3d1-47db-bef7-03118826849c".getBytes());//.get("shiro:session:6385811a-7498-45cf-abb9-cda2f9339864");
        Session session = byteToSession(hello);

        System.out.println("----------" + hello);
        resource.close();
    }

    // 把byte还原为session
    public static Session byteToSession(byte[] bytes) {
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
        ObjectInputStream in;
        SimpleSession session = null;
        try {
            in = new ObjectInputStream(bi);
            session = (SimpleSession) in.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return session;
    }

    /**
     * 这个用户代码表信息系统
     *
     * @return
     */
    public static UserVO getSystem() {
        return new UserVO();
//	    	return  new UserVO("-1", "systemSelf", null, null, null, null, "100201", "1", null, "-1", "1", "信息系统", "信息系统", null, null, "1", null, null, 0, null, 5, null, (long) 1, null, "1", "blue", "-1", "1", "1","1","1","","","","","");
    }

}
